package com.bosssoft.g1.authserver.domain;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import com.bosssoft.g1.authserver.entity.User;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 登录用户信息
 * Created by macro on 2020/6/19.
 */
@Data
@ToString
public class SecurityUser implements UserDetails {

    /**
     * 工号
     */
    private Long id;

    /**
     * 名字
     */
    private String name;

    /**
     * 名字
     */
    private String code;

    /**
     * 名字
     */
    private String password;

    /**
     * 名字
     */
    private Long orgId;

    /**
     * 名字
     */
    private Long tenantId;

    /**
     * 名字
     */
    private Long companyId;

    /**
     * 名字
     */
    private Byte status;

    /**
     * 名字
     */
    private boolean enabled;

    /**
     * 权限数据
     */
    private Collection<SimpleGrantedAuthority> authorities;

    public SecurityUser() {

    }

    public SecurityUser(User user, List<String> roleNames) {
//        this.setId(userDTO.getId());
//        this.setUsername(userDTO.getUsername());
//        this.setPassword(userDTO.getPassword());
//        this.setEnabled(userDTO.getStatus() == 1);
//        if (userDTO.getRoles() != null) {
//            authorities = new ArrayList<>();
//            userDTO.getRoles().forEach(item -> authorities.add(new SimpleGrantedAuthority(item)));
//        }
        if (user == null) {
            throw new IllegalArgumentException("User cannot be null");
        }

        if (roleNames == null) {
            roleNames = new ArrayList<>();
        }

        BeanUtil.copyProperties(user, this);
        this.setId(user.getId());
        this.setCode(user.getCode());
        this.setPassword(user.getPassword());
        this.setEnabled(user.getStatus() == 1);
        if (CollectionUtil.isNotEmpty(roleNames)) {
            authorities = new ArrayList<>();
            roleNames.forEach(item -> authorities.add(new SimpleGrantedAuthority(item)));
        }
        System.out.println(this);
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.code;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

}
